package com.alstudio.module.xmpp.protocol.xmpp.parser.iq.usersaction;

import java.util.ArrayList;

import com.alstudio.module.xmpp.protocol.xmpp.ALXmppEventListener;
import com.alstudio.module.xmpp.protocol.xmpp.packact.ALIQ;
import com.alstudio.module.xmpp.protocol.xmpp.parser.iq.ALBaseIQParser;
import com.alstudio.module.xmpp.protocol.xmpp.parser.iq.IQParseEventHandler.IQXmlParseEventCallback;
import com.alstudio.pojo.module.user.User;
import com.alstudio.utils.formater.numeric.ALNumericUtils;

/**
 * 解析黑名单
 * 
 * @author alonso lee
 * 
 */
public class CheckBlackListParser extends ALBaseIQParser implements
		IQXmlParseEventCallback {

	private ArrayList<User> users;
	private User user;

	@Override
	public void parseIQPackage(ALIQ packact, String stream,
			ALXmppEventListener listener) throws Exception {
		// TODO Auto-generated method stub
		code = 0;
		user = null;
		if (users != null) {
			users.clear();
			users = null;
		}
		users = new ArrayList<User>();
		mListener = listener;
		startParse(packact, stream, this);
	}

	@Override
	public void processErrorStartTag(String tag) {
		// TODO Auto-generated method stub
		if (tag.equals(ERROR_CODE_TAG)) {
			code = ALNumericUtils.parseInt(getAttValue("code"), 0);
		}
	}

	@Override
	public void processSetStartTag(String tag) {
		// TODO Auto-generated method stub

	}

	@Override
	public void processGetStartTag(String tag) {
		// TODO Auto-generated method stub

	}

	@Override
	public void processResultStartTag(String tag) {
		// TODO Auto-generated method stub
		if (tag.equals(ITEM_TAG)) {
			user = new User();
			user.setJid(getAttValue("value"));
			user.setNick(getAttValue("nick"));
			user.setImageFileId(getAttValue("avatar"));
			users.add(user);
		}
	}

	@Override
	public void processEndTag(String tag) {
		// TODO Auto-generated method stub

	}

	@Override
	public void processStartDocument() {
		// TODO Auto-generated method stub

	}

	@Override
	public void processEndDocument() {
		// TODO Auto-generated method stub
		onCallback();
	}

	@Override
	public void onCallback() {
		// TODO Auto-generated method stub
		if (mListener != null) {
			mListener.onGetBlackListResult(code, users);
		}
	}

}
